package com.hssoftvn.android.apps.tinycouplechat;

import java.nio.charset.Charset;
import java.security.PublicKey;
import java.util.Calendar;

import org.apache.http.HttpConnection;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.os.AsyncTask;


/*AsyncTask<ChatItem, Void, Integer>
 * 	ChatItem: data we want to handle, Parameter for method execute(...) 
 * 	Void : don't care, less using it, let it Void
 * 	Integer: the return values after task is done:
 * 		- Successful or Failed
 * 		- Any ENUM value to be a return value
 * 		- Useful to display some popup or error dialog to user
 */


public class ChatItemSender extends AsyncTask<ChatItem, Integer, Integer>
{

	static Calendar mCalendar = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    
    /*
     *  RETURN VALUES:
     * 		SUCCESSFUL: sent ok
     *		NETWORK_ERROR: network error
     */
    
    @Override
    protected Integer doInBackground(ChatItem... params) {
    	if (Utils.getApplication().getMyInfo().mUsername.equals("")
    			|| Utils.getApplication().getCouplexInfo().mUsername.equals(""))
    	{
    		cancel(false);
    		return 0;
    	}
        // this method will be running on background thread so don't update UI frome here
        // do your long running http tasks here, 
    	// you dont want to pass argument and u can access the parent class' variable url over here
    	/*
    	 * HANDLING MESSAGE STATUS HERE
    	 */
    	
    	final int count = params.length;
    	Integer a_status[] = new Integer[count];
    	String requests[] = new String[count];
    	String responses[] = new String[count];

    	if (mCalendar==null)
    		mCalendar = Calendar.getInstance();
    	try{
    		
	    	for(int i=0; i< count; i++){
	    		//Init status
	    		a_status[i] = Utils.SENDING_STATUS_SENDING;
	    		Calendar c = Calendar.getInstance(); 
	    		int h = c.get(Calendar.HOUR);
	    		int m = c.get(Calendar.MINUTE);
	    		params[i].mSentTime= h + ":" + m;
	    		String url = NetworkHelper.URL_SENDING_MSG;
	    		url = url.replace("@FROMID",Utils.getApplication().getMyInfo().mUsername);
	    		url = url.replace("@TOID",Utils.getApplication().getCouplexInfo().mUsername);
	    		
	    		String msg = Utils.encodeMessage(params[i].getText());
	    		url = url.replace("@MESSAGE",msg);
	    		url = url.replace("@SENTTIME","0");
	    		url = url.replace("@STATUS","0");
	    		url = url.replace("@LOCATION","0,0");
	    		requests[i] = url;
	    		
	    		//Requests
	    		responses[i] = "";
	
	    		//Update sending status
	    		params[i].setStatus(a_status[i]);
	    		publishProgress(a_status);
	    		
	    		responses[i] = NetworkHelper.getHttpResponse(requests[i]);
	    		
	    		//Update sending status
	    		a_status[i] = Utils.SENDING_STATUS_DELEVERED;
	    		h = c.get(Calendar.HOUR);
	    		m = c.get(Calendar.MINUTE);
	    		params[i].mSentTime= h + ":" + m;	    		
	    		params[i].setStatus(a_status[i]);
	    		
	    		publishProgress(a_status); 
	    	}   
    	
    	} catch(Exception ex){    		
    	}
    	
        return 0;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

    	Utils.getApplication().checkNewMessages();
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
    	// TODO Auto-generated method stub
    	super.onProgressUpdate(values);
    	Utils.getApplication().notifyDataChanged();
    }        
}
